package com.pioneer.controller.client;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.common.utils.SecurityUtils;
import com.pioneer.web.client.domain.CliCar;
import com.pioneer.web.client.service.ICliCarService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 通知公告
 *
 * @author hlm
 */
@RestController
@RequestMapping("/client/car")
public class CliCarController extends BaseController {

    @Resource
    private ICliCarService carService;

    /**
     * 获取通知公告列表
     *
     * @param car 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:car:list')")
    @GetMapping("/list")
    public AjaxResult list(CliCar car) {
        boolean isPage = startPage();
        car.setClientId(SecurityUtils.getUserId());
        List<CliCar> list = carService.carList(car);
        return success(isPage, list);
    }

    /**
     * 根据通知公告id获取详细信息
     *
     * @param carId 通知公告id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:car:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable Long carId) {
        return AjaxResult.success(carService.getCarById(carId));
    }

    /**
     * 新增通知公告
     *
     * @param car 通知公告
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:car:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CliCar car) {
        return toAjax(carService.save(car));
    }

    /**
     * 修改通知公告
     *
     * @param car 通知公告
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:car:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CliCar car) {
        return toAjax(carService.updateById(car));
    }

    /**
     * 删除通知公告
     *
     * @param carIds 通知公告id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:car:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds) {
        return toAjax(carService.removeByIds(Arrays.asList(carIds)));
    }

    /**
     * 下单后删除购物车
     *
     * @param carId 购物车id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('client:car:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeAfterOrder/{carId}")
    public AjaxResult removeAfterOrder(@PathVariable Long carId) {
        return toAjax(carService.removeAfterOrder(carId));
    }
}
